/*
 * Copyright (c) Hisilicon Technologies Co., Ltd. 2019-2019. All rights reserved.
 * Description: This file is used for adas parameter setting
 * Author:  HiMobileCam Reference Develop Team
 * Create: 2019/11/08
 */



#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "ui_common.h"

#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif
#endif /*  __cplusplus  */
typedef struct {
    HI_BOOL adasEnable;
    HI_BOOL adasFCWEnable;
    HI_BOOL adasLDWEnable;
    HI_BOOL adasFCREnable;
    HI_BOOL setFCW;
    HI_BOOL setFCR;
    HI_BOOL setLDW;
    HI_BOOL adasCalib;
    HIGV_HANDLE curFocus;
}UI_ADAS_Ctx;

static UI_ADAS_Ctx g_adasPageCtx = {.curFocus = ADAS_ENABLE_BUTTON,};

static HI_BOOL PDT_UI_GetADASParam(HI_PDT_PARAM_TYPE_E type)
{
    HI_BOOL value = HI_FALSE;
    HI_PDT_STATEMNG_WORKMODE_STATE_S workState;
    HI_S32 ret = HI_PDT_STATEMNG_GetState(&workState);
    HI_APPCOMM_CHECK_RETURN(ret, ret);
    HI_U32 camID = HI_PDT_STATEMNG_GetPreViewCamID();
    ret = HI_PDT_PARAM_GetCamParam(workState.enWorkMode, camID, type, &value);
    HI_APPCOMM_CHECK_RETURN(ret, ret);
    return value;
}

static HI_S32 PDT_UI_SetADASSkin(HIGV_HANDLE WidgetHdl, HI_BOOL enable)
{
    HI_S32 ret;
    ret = HI_GV_Widget_SetSkin(WidgetHdl, HIGV_SKIN_NORMAL,
        enable ? SKIN_DIALOG_FORMAT_CHOSED_NORMAL : SKIN_DIALOG_FORMAT_NOT_CHOSED_NORMAL);
    HI_APPCOMM_CHECK_RETURN(ret, ret);
    ret = HI_GV_Widget_SetSkin(WidgetHdl, HIGV_SKIN_ACITVE,
        enable ? SKIN_DIALOG_FORMAT_CHOSED_ACTIVE : SKIN_DIALOG_FORMAT_NOT_CHOSED_ACTIVE);
    HI_APPCOMM_CHECK_RETURN(ret, ret);
    ret = HI_GV_Widget_Paint(WidgetHdl, NULL);
    HI_APPCOMM_CHECK_RETURN(ret, ret);
    return ret;
}

static HI_S32 PDT_UI_SetADASParamEnable(HI_VOID)
{
    HI_S32 ret;
    g_adasPageCtx.adasFCWEnable = PDT_UI_GetADASParam(HI_PDT_PARAM_TYPE_ADAS_FCW);
    g_adasPageCtx.adasFCREnable = PDT_UI_GetADASParam(HI_PDT_PARAM_TYPE_ADAS_FCR);
    g_adasPageCtx.adasLDWEnable = PDT_UI_GetADASParam(HI_PDT_PARAM_TYPE_ADAS_LDW);
    g_adasPageCtx.setFCW = g_adasPageCtx.adasFCWEnable;
    g_adasPageCtx.setFCR = g_adasPageCtx.adasFCREnable;
    g_adasPageCtx.setLDW = g_adasPageCtx.adasLDWEnable;
    MLOGD("fcw:%d,fcr:%d,ldw:%d\n",g_adasPageCtx.adasFCWEnable, g_adasPageCtx.adasFCREnable, g_adasPageCtx.adasLDWEnable);
    ret = PDT_UI_SetADASSkin(ADAS_FCW_BUTTON, g_adasPageCtx.adasFCWEnable);
    HI_APPCOMM_CHECK_RETURN(ret, ret);
    ret = PDT_UI_SetADASSkin(ADAS_FCR_BUTTON, g_adasPageCtx.adasFCREnable);
    HI_APPCOMM_CHECK_RETURN(ret, ret);
    ret = PDT_UI_SetADASSkin(ADAS_LDW_BUTTON, g_adasPageCtx.adasLDWEnable);
    HI_APPCOMM_CHECK_RETURN(ret, ret);
    return HI_SUCCESS;
}

static HI_S32 PDT_UI_ADASFocusToNext(HIGV_HANDLE WidgetHdl)
{
    HI_S32 ret;
    if(PDT_UI_GetADASParam(HI_PDT_PARAM_TYPE_ADAS_STATUS) == HI_TRUE) {
        if (WidgetHdl == ADAS_ENABLE_BUTTON) {
            ret = HI_GV_Widget_Active(ADAS_CALIBRATE_BUTTON);
            HI_APPCOMM_CHECK_RETURN(ret, HIGV_PROC_GOON);
        }else if(WidgetHdl == ADAS_CALIBRATE_BUTTON) {
            ret = HI_GV_Widget_Active(ADAS_FCW_BUTTON);
            HI_APPCOMM_CHECK_RETURN(ret, HIGV_PROC_GOON);
        }else if(WidgetHdl == ADAS_FCW_BUTTON) {
            ret = HI_GV_Widget_Active(ADAS_FCR_BUTTON);
            HI_APPCOMM_CHECK_RETURN(ret, HIGV_PROC_GOON);
        }else if(WidgetHdl == ADAS_FCR_BUTTON) {
            ret = HI_GV_Widget_Active(ADAS_LDW_BUTTON);
            HI_APPCOMM_CHECK_RETURN(ret, HIGV_PROC_GOON);
        }else if(WidgetHdl == ADAS_LDW_BUTTON) {
            ret = HI_GV_Widget_Active(ADAS_CANCEL);
            HI_APPCOMM_CHECK_RETURN(ret, HIGV_PROC_GOON);
        }else if(WidgetHdl == ADAS_CANCEL) {
            ret = HI_GV_Widget_Active(ADAS_CONFIRM);
            HI_APPCOMM_CHECK_RETURN(ret, HIGV_PROC_GOON);
        }else if(WidgetHdl == ADAS_CONFIRM) {
            ret = HI_GV_Widget_Active(ADAS_ENABLE_BUTTON);
            HI_APPCOMM_CHECK_RETURN(ret, HIGV_PROC_GOON);
        }
    }else {
        if (WidgetHdl == ADAS_ENABLE_BUTTON) {
            ret = HI_GV_Widget_Active(ADAS_CANCEL);
            HI_APPCOMM_CHECK_RETURN(ret, HIGV_PROC_GOON);
        }else if(WidgetHdl == ADAS_CANCEL) {
            ret = HI_GV_Widget_Active(ADAS_CONFIRM);
            HI_APPCOMM_CHECK_RETURN(ret, HIGV_PROC_GOON);
        }else if(WidgetHdl == ADAS_CONFIRM) {
            ret = HI_GV_Widget_Active(ADAS_ENABLE_BUTTON);
            HI_APPCOMM_CHECK_RETURN(ret, HIGV_PROC_GOON);
        }
    }
    return HI_SUCCESS;
}

static HI_S32 PDT_UI_ADASFocusToPre(HIGV_HANDLE WidgetHdl)
{
    HI_S32 ret;
    if(PDT_UI_GetADASParam(HI_PDT_PARAM_TYPE_ADAS_STATUS) == HI_TRUE) {
        if (WidgetHdl == ADAS_ENABLE_BUTTON) {
            ret = HI_GV_Widget_Active(ADAS_CONFIRM);
            HI_APPCOMM_CHECK_RETURN(ret, HIGV_PROC_GOON);
        }else if(WidgetHdl == ADAS_CALIBRATE_BUTTON) {
            ret = HI_GV_Widget_Active(ADAS_ENABLE_BUTTON);
            HI_APPCOMM_CHECK_RETURN(ret, HIGV_PROC_GOON);
        }else if(WidgetHdl == ADAS_FCW_BUTTON) {
            ret = HI_GV_Widget_Active(ADAS_CALIBRATE_BUTTON);
            HI_APPCOMM_CHECK_RETURN(ret, HIGV_PROC_GOON);
        }else if(WidgetHdl == ADAS_FCR_BUTTON) {
            ret = HI_GV_Widget_Active(ADAS_FCW_BUTTON);
            HI_APPCOMM_CHECK_RETURN(ret, HIGV_PROC_GOON);
        }else if(WidgetHdl == ADAS_LDW_BUTTON) {
            ret = HI_GV_Widget_Active(ADAS_FCR_BUTTON);
            HI_APPCOMM_CHECK_RETURN(ret, HIGV_PROC_GOON);
        }else if(WidgetHdl == ADAS_CANCEL) {
            ret = HI_GV_Widget_Active(ADAS_LDW_BUTTON);
            HI_APPCOMM_CHECK_RETURN(ret, HIGV_PROC_GOON);
        }else if(WidgetHdl == ADAS_CONFIRM) {
            ret = HI_GV_Widget_Active(ADAS_CANCEL);
            HI_APPCOMM_CHECK_RETURN(ret, HIGV_PROC_GOON);
        }
    }else {
        if (WidgetHdl == ADAS_ENABLE_BUTTON) {
            ret = HI_GV_Widget_Active(ADAS_CONFIRM);
            HI_APPCOMM_CHECK_RETURN(ret, HIGV_PROC_GOON);
        }else if(WidgetHdl == ADAS_CANCEL) {
            ret = HI_GV_Widget_Active(ADAS_ENABLE_BUTTON);
            HI_APPCOMM_CHECK_RETURN(ret, HIGV_PROC_GOON);
        }else if(WidgetHdl == ADAS_CONFIRM) {
            ret = HI_GV_Widget_Active(ADAS_CANCEL);
            HI_APPCOMM_CHECK_RETURN(ret, HIGV_PROC_GOON);
        }
    }
    return HI_SUCCESS;
}

HI_S32 PDT_UI_ADASPageExit(HI_VOID)
{
    g_adasPageCtx.curFocus = ADAS_ENABLE_BUTTON;
    HI_S32 ret = PDT_UI_WINMNG_FinishWindow(ADAS_WINDOW);
    HI_APPCOMM_CHECK_RETURN_WITH_ERRINFO(ret, ret, "PDT_UI_WINMNG_FinishWindow");
    return HI_SUCCESS;
}

static HI_S32 PDT_UI_OnReceiveMsgResult(HI_EVENT_S* event)
{
    HI_S32 ret = PDT_UI_WINMNG_FinishWindow(BUSY_PAGE);
    HI_APPCOMM_CHECK_RETURN(ret, ret);
    return HI_SUCCESS;
}

static HI_S32 PDT_UI_SetAdasEnable()
{
    HI_S32 ret;
    HI_MESSAGE_S message;
    HI_PDT_STATEMNG_WORKMODE_STATE_S workState;
    HI_BOOL oldValue = PDT_UI_GetADASParam(HI_PDT_PARAM_TYPE_ADAS_STATUS);
    g_adasPageCtx.adasEnable = (oldValue == HI_TRUE) ? HI_FALSE : HI_TRUE;

    if(g_adasPageCtx.adasEnable == HI_TRUE) {
        ret = HI_GV_Widget_Show(ADAS_CALIBRATE_BUTTON);
        HI_APPCOMM_CHECK_RETURN(ret, HI_FAILURE);
        ret = HI_GV_Widget_Show(ADAS_PARAM_GROUPBOX);
        HI_APPCOMM_CHECK_RETURN(ret, HI_FAILURE);
        ret = PDT_UI_SetADASParamEnable();
        HI_APPCOMM_CHECK_RETURN(ret, ret);
    }else {
        ret = HI_GV_Widget_Hide(ADAS_CALIBRATE_BUTTON);
        HI_APPCOMM_CHECK_RETURN(ret, HI_FAILURE);
        ret = HI_GV_Widget_Hide(ADAS_PARAM_GROUPBOX);
        HI_APPCOMM_CHECK_RETURN(ret, HI_FAILURE);
    }
    if(g_adasPageCtx.adasEnable == HI_FALSE) {
        PDT_UI_SetAdasCalibStatus(HI_FALSE);
    }
    ret = HI_PDT_STATEMNG_GetState(&workState);
    HI_APPCOMM_CHECK_RETURN(ret, HI_FAILURE);
    memset_s(&message, sizeof(HI_MESSAGE_S), 0x0, sizeof(HI_MESSAGE_S));

    message.what = HI_EVENT_STATEMNG_ADAS_ENABLE;
    message.arg2 = workState.enWorkMode;
    ret = memcpy_s(message.aszPayload, sizeof(message.aszPayload), &g_adasPageCtx.adasEnable, sizeof(HI_BOOL));
    ret = PDT_UI_WINMNG_StartWindow(BUSY_PAGE, HI_FALSE);
    HI_APPCOMM_CHECK_RETURN(ret, HI_FAILURE);
    ret = PDT_UI_COMM_SendAsyncMsg(&message, PDT_UI_OnReceiveMsgResult);
    if(ret != HI_SUCCESS) {
        ret = PDT_UI_WINMNG_FinishWindow(BUSY_PAGE);
        HI_APPCOMM_CHECK_RETURN(ret, HI_FAILURE);
        return HI_FAILURE;
    }

    return HI_SUCCESS;
}
static HI_S32 PDT_UI_ADASOnClick(HIGV_HANDLE WidgetHdl)
{
    HI_S32 ret;
    if (WidgetHdl == ADAS_ENABLE_BUTTON) {
        ret = PDT_UI_SetAdasEnable();
        HI_APPCOMM_CHECK_RETURN(ret, ret);
        ret = PDT_UI_SetADASSkin(WidgetHdl,g_adasPageCtx.adasEnable);
        HI_APPCOMM_CHECK_RETURN(ret, ret);
    }else if(WidgetHdl == ADAS_CALIBRATE_BUTTON) {
        if(!g_adasPageCtx.adasFCWEnable && !g_adasPageCtx.adasFCREnable && !g_adasPageCtx.adasLDWEnable) {
            MLOGW("please start algo detect type,and then to do calibrateion!\n");
            return HI_SUCCESS;
        }else {
            ret = PDT_UI_WINMNG_StartWindow(ADAS_CALIBRATE_WINDOW, HI_TRUE);
            HI_APPCOMM_CHECK_RETURN(ret, HI_FAILURE);
            g_adasPageCtx.curFocus = ADAS_CALIBRATE_BUTTON;
        }
    }else if(WidgetHdl == ADAS_FCW_BUTTON) {
        g_adasPageCtx.setFCW = (g_adasPageCtx.setFCW == HI_TRUE) ? HI_FALSE : HI_TRUE;
        PDT_UI_SetADASSkin(WidgetHdl,g_adasPageCtx.setFCW);
    }else if(WidgetHdl == ADAS_FCR_BUTTON) {
        g_adasPageCtx.setFCR = (g_adasPageCtx.setFCR == HI_TRUE) ? HI_FALSE : HI_TRUE;
        PDT_UI_SetADASSkin(WidgetHdl,g_adasPageCtx.setFCR);
    }else if(WidgetHdl == ADAS_LDW_BUTTON) {
        g_adasPageCtx.setLDW = (g_adasPageCtx.setLDW == HI_TRUE) ? HI_FALSE : HI_TRUE;
        PDT_UI_SetADASSkin(WidgetHdl,g_adasPageCtx.setLDW);
    }else if(WidgetHdl == ADAS_CANCEL) {
        ret = PDT_UI_ADASPageExit();
        HI_APPCOMM_CHECK_RETURN_WITH_ERRINFO(ret, HIGV_PROC_GOON, "PDT_UI_ADASPageExit");
    } else if(WidgetHdl == ADAS_CONFIRM) {
        if(!g_adasPageCtx.adasEnable) {
            MLOGI("adas not enable!\n");
        } else {
            HI_PDT_STATEMNG_AdasDetectAttr attr;
            HI_MESSAGE_S message = {0};
            memset_s(&message, sizeof(HI_MESSAGE_S), 0x0, sizeof(HI_MESSAGE_S));
            attr.fcwEnable = g_adasPageCtx.setFCW;
            attr.fcrEnable = g_adasPageCtx.setFCR;
            attr.ldwEnable = g_adasPageCtx.setLDW;
            message.what = HI_EVENT_STATEMNG_ADAS_SET_ATTR;
            memcpy_s(message.aszPayload, sizeof(message.aszPayload), &attr, sizeof(HI_PDT_STATEMNG_AdasDetectAttr));
            ret = HI_PDT_STATEMNG_SendMessage(&message);
            HI_APPCOMM_CHECK_RETURN(ret, ret);
        }
        ret = PDT_UI_WINMNG_FinishWindow(ADAS_WINDOW);
        HI_APPCOMM_CHECK_RETURN_WITH_ERRINFO(ret, HIGV_PROC_GOON, "PDT_UI_WINMNG_FinishWindow");
    }
    return HI_SUCCESS;
}

HI_S32 PDT_UI_ADASOnShow(HIGV_HANDLE WidgetHdl, HI_PARAM wParam, HI_PARAM lParam)
{
    HI_S32 ret;
    g_adasPageCtx.adasEnable = PDT_UI_GetADASParam(HI_PDT_PARAM_TYPE_ADAS_STATUS);
    if(!g_adasPageCtx.adasEnable) {
        ret = HI_GV_Widget_Hide(ADAS_CALIBRATE_BUTTON);
        HI_APPCOMM_CHECK_RETURN(ret, ret);
        ret = HI_GV_Widget_Hide(ADAS_PARAM_GROUPBOX);
        HI_APPCOMM_CHECK_RETURN(ret, ret);
    }else {
        ret = PDT_UI_SetADASSkin(ADAS_ENABLE_BUTTON, g_adasPageCtx.adasEnable);
        HI_APPCOMM_CHECK_RETURN(ret, ret);
        ret = PDT_UI_SetADASParamEnable();
        HI_APPCOMM_CHECK_RETURN(ret, ret);
    }
    ret = HI_GV_Widget_Active(g_adasPageCtx.curFocus);
    HI_APPCOMM_CHECK_RETURN(ret, ret);

    return HIGV_PROC_GOON;
}

HI_S32 PDT_UI_ADASOnKeyDown(HIGV_HANDLE WidgetHdl, HI_PARAM wParam, HI_PARAM lParam)
{
    HI_S32 ret;
    MLOGD("wParam: %#lx, lParam = %#lx\n", (HI_UL)wParam, (HI_UL)lParam);
    if (wParam == PDT_UI_KEY_BACK) {
        ret = PDT_UI_ADASPageExit();
        HI_APPCOMM_CHECK_RETURN_WITH_ERRINFO(ret, HIGV_PROC_GOON, "PDT_UI_ADASPageExit");
    }else if(wParam == PDT_UI_KEY_MENU) {
        PDT_UI_ADASFocusToNext(WidgetHdl);
    }else if(wParam == PDT_UI_KEY_EXPAND) {
        PDT_UI_ADASFocusToPre(WidgetHdl);
    }else if(wParam == PDT_UI_KEY_OK) {
        PDT_UI_ADASOnClick(WidgetHdl);
    }

    return HIGV_PROC_GOON;
}

HI_S32 PDT_UI_ADASOnGestureTap(HIGV_HANDLE WidgetHdl, HI_PARAM wParam, HI_PARAM lParam)
{
    HI_S32 ret;
    if(WidgetHdl == ADAS_ENABLE_BUTTON) {
        ret = HI_GV_Widget_Active(ADAS_ENABLE_BUTTON);
        HI_APPCOMM_CHECK_RETURN(ret, ret);
    }else if(WidgetHdl == ADAS_CALIBRATE_BUTTON) {
        ret = HI_GV_Widget_Active(ADAS_CALIBRATE_BUTTON);
        HI_APPCOMM_CHECK_RETURN(ret, ret);
    }else if(WidgetHdl == ADAS_FCW_BUTTON) {
        ret = HI_GV_Widget_Active(ADAS_FCW_BUTTON);
        HI_APPCOMM_CHECK_RETURN(ret, ret);
    }else if(WidgetHdl == ADAS_FCR_BUTTON) {
        ret = HI_GV_Widget_Active(ADAS_FCR_BUTTON);
        HI_APPCOMM_CHECK_RETURN(ret, ret);
    }else if(WidgetHdl == ADAS_LDW_BUTTON) {
        ret = HI_GV_Widget_Active(ADAS_LDW_BUTTON);
        HI_APPCOMM_CHECK_RETURN(ret, ret);
    }else if(WidgetHdl == ADAS_CANCEL) {
        ret = HI_GV_Widget_Active(ADAS_CANCEL);
        HI_APPCOMM_CHECK_RETURN(ret, ret);
    }else if(WidgetHdl == ADAS_CONFIRM) {
        ret = HI_GV_Widget_Active(ADAS_CONFIRM);
        HI_APPCOMM_CHECK_RETURN(ret, ret);
    }
    ret = PDT_UI_ADASOnClick(WidgetHdl);
    HI_APPCOMM_CHECK_RETURN(ret, ret);
    return HIGV_PROC_GOON;
}

#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif /*  __cplusplus  */

